package hfut.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hfut.example.common.CustomException;
import hfut.example.common.R;
import hfut.example.dto.SetmealDto;
import hfut.example.entity.Setmeal;
import hfut.example.entity.SetmealDish;
import hfut.example.mapper.SetmealMapper;
import hfut.example.service.SetmealDishService;
import hfut.example.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐及其与餐品的关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐
        this.save(setmealDto);

        //补充setmealDish的setmealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //setmealDishes=setmealDishes.stream().peek((item)-> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
        setmealDishes.stream().peek((item)-> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
        //保存套餐菜品关联关系
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐及其与餐品的关系
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        // 判断是否可以删除
        if(count>0){
            throw new CustomException("套餐正在售卖中,无法删除");
        }

        //先删除套餐表
        this.removeByIds(ids);
        //再删除关系表
        LambdaQueryWrapper<SetmealDish> queryWrapperDish = new LambdaQueryWrapper<>();
        queryWrapperDish.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapperDish);
    }

    @Override
    @Transactional
    public R<String> updateWithSetmealDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        Long setmealId = setmealDto.getId();
        //先删除套餐与菜品关系
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);
        //再添加套餐与菜品关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().peek((item)->item.setSetmealId(setmealId)).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
        return R.success("套餐更新成功!");
    }
}
